<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    #print{
      display: none;
    }
    #noPrint{
      display: block;
    }
  </style>
  <style media="print">
    #noPrint{
      display: none;
    }
    #print{
      display: block;
    }
    @page {
      size: A4;
    }
  </style>
</head>
<body>
  <div id="workspace">
    <div id="music1">
      <h2>水调歌头</h2>
  
      <p>明月几时有？把酒问青天。</p>
      <p>不知天上宫阙，今夕是何年。</p>
      <p>我欲乘风归去，又恐琼楼玉宇，高处不胜寒。</p>
      <p>起舞弄轻影，何似在人间。</p>
    </div>
  
    <button onclick="printAll()">打印全部</button>
    <button onclick="printM1()">打印《水调歌头》</button>

    <div id="noPrint"></div>
    <div id="print"></div>
  </div>

  <script>
    function printAll () {
      console.log('print all')
      window.print()
    }

    function printM1 () {
      console.log('M1')
      var bodyInnerHtml = document.querySelector('#workspace').innerHTML
      var m1InnerHtml = document.querySelector('#music1').innerHTML
      
      document.querySelector('#workspace').innerHTML = ''
      document.querySelector('#workspace').innerHTML = '<div id="noPrint"></div><div id="print"></div>'
      document.querySelector('#print').innerHTML = m1InnerHtml
      document.querySelector('#noPrint').innerHTML = bodyInnerHtml
      window.print()
      document.querySelector('#workspace').innerHTML = document.querySelector('#noPrint').innerHTML
      document.querySelector('#print').innerHTML = ''
    }
  </script>
</body>
</html>